
package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.InitialContext;
import javax.sql.DataSource;

import exception.BaseException;


public class DBConnection {
	
	/**
	 * 
	 */
	public static final String ATTEND 		= "PHILERGYSYSTEM_ATTEND";	//근태관리
	public static final String LEASE_INFO 	= "LEASE_INFO"; 			//서강대학교

	static Connection con; 
	
	public static Connection Connection() throws BaseException {
		try {
			String driver = "net.sourceforge.jtds.jdbc.Driver";

//			String url = "jdbc:jtds:sqlserver://192.168.10.109:1433;DatabaseName=LEASE_INFO";
//			String user = "smb";
//			String pass = "phil8642";
			
			String url = "jdbc:jtds:sqlserver://localhost:1434;DatabaseName=LEASE_INFO";
			String user = "sa";
			String pass = "sql2005";

			Class.forName(driver);
		    
		    con = DriverManager.getConnection(url, user, pass);
		} catch(SQLException se) {
			
	    } catch (ClassNotFoundException e) {
	    	e.printStackTrace();
	    }
	    
	    return con;
	}
	
	/**
	 * <pre>
	 * 	근태관리 시스템이 추가되어
	 * 	선택적으로 사용할 수 있게 하기 위함.
	 * </pre>
	 * @author 양창환
	 * @since  2009-01-07
	 * @param  String -데이터베이스 명-
	 * @return java.sql.Connection -커넥션 객체-
	 * @throws BaseException
	 */
	public static Connection Connection(String dbName) throws BaseException {
		try {
			String driver = "net.sourceforge.jtds.jdbc.Driver";

//			String url = "jdbc:jtds:sqlserver://192.168.10.109:1433;DatabaseName=LEASE_INFO";
//			String user = "smb";
//			String pass = "phil8642";
			
			String url = "jdbc:jtds:sqlserver://localhost:1434;DatabaseName=" + dbName;	//데이터베이스 선택.
			String user = "sa";
			String pass = "sql2005";

			Class.forName(driver);
		    
		    con = DriverManager.getConnection(url, user, pass);
		} catch(SQLException se) {
			
	    } catch (ClassNotFoundException e) {
	    	e.printStackTrace();
	    }
	    
		return con;
	}
	
	/**
	 * <pre>
	 * 	근태관리 시스템이 추가되어
	 * 	선택적으로 사용할 수 있게 하기 위함.
	 *  Tomcat에 DBCP 적용
	 * </pre>
	 * @author 양창환
	 * @since  2009-01-19
	 * @return
	 * @throws BaseException
	 */
	public static DataSource getDataSource() throws BaseException {
		
		DataSource ds = null;
		
		try {
			InitialContext ctx = new InitialContext();
			ds = (DataSource)ctx.lookup("java:/comp/env/jdbc/PHILSYS");
			
		} catch (Exception e) {
			BaseException eex = new BaseException();          // DaoException 생성
			eex.setException(e);                              // AppReturnCode 셋팅
			throw eex;     	
		}
		return ds;
	}
	
	public void close(ResultSet rs){
		try {
			if(rs != null) rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void close(PreparedStatement pstmt){
		try {
			if(pstmt != null) pstmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void close(Statement stmt){
		try {
			if(stmt != null) stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void close(Connection con){
		try {
			if(con != null) con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
